import 'package:dio/dio.dart';
import 'package:edu/constants/config.dart';
import 'package:edu/networks/business/models/business_response.dart';
import 'package:edu/networks/common/base_response.dart';
import 'package:retrofit/retrofit.dart';

part 'business_api.g.dart';

abstract class BusinessApi {
  // 课程评价列表
  Future<CourseReviewListResponse> getCourseReviews({
    required int courseId,
    bool fetchMyReview = false,
    int? page,
    int? pageSize,
    CancelToken? cancelToken,
  });

  // 创建课程评价
  Future<MyReviewResponse> reviewCourse(
    int courseId,
    int rating,
    String content,
  );

  // 创建课程评价回复
  Future<SimpleResponse> replyCourseReview(
    int courseId,
    int reviewId,
    String content,
  );

  // 添加课程收藏
  Future<SimpleResponse> addFavoriteCourse(int courseId);

  // 删除课程收藏
  Future<SimpleResponse> removeFavoriteCourse(int courseId);

  // 添加课程到购物车
  Future<SimpleResponse> addCourseToShoppingCart(int courseId, double price);

  // 删除购物车中的课程
  Future<PurchaseCourseResponse> removeCourseFromShoppingCart(
    List<int> courseIds,
  );

  // 购买课程
  Future<PurchaseCourseResponse> purchaseCourse(List<int> courseIds);

  // 获取课程订单
  Future<CourseOrderListResponse> getCourseOrders({
    required List<int> courseIds,
    CancelToken? cancelToken,
  });

  // 正在购买的课程列表
  Future<PurchasingCourseListResponse> getPurchasingCourses({
    int? offset,
    int? page,
    int? pageSize,
    CancelToken? cancelToken,
  });
}

@RestApi(baseUrl: Config.baseUrl)
abstract class RemoteBusinessApi extends BusinessApi {
  factory RemoteBusinessApi(Dio dio, {String baseUrl}) = _RemoteBusinessApi;

  // 课程评价列表
  @override
  @GET('/v1/course/reviews')
  Future<CourseReviewListResponse> getCourseReviews({
    @Query('course_id') required int courseId,
    @Query('fetch_my_review') bool fetchMyReview = false,
    @Query('page') int? page,
    @Query('page_size') int? pageSize,
    @CancelRequest() CancelToken? cancelToken,
  });

  // 创建课程评价
  @override
  @POST('/v1/course/review')
  Future<MyReviewResponse> reviewCourse(
    @Field('course_id') int courseId,
    @Field('rating') int rating,
    @Field('content') String content,
  );

  // 创建课程评价回复
  @override
  @POST('/v1/course/review/reply')
  Future<SimpleResponse> replyCourseReview(
    @Field('course_id') int courseId,
    @Field('review_id') int reviewId,
    @Field('content') String content,
  );

  // 添加课程收藏
  @override
  @POST('/v1/course/favorite')
  Future<SimpleResponse> addFavoriteCourse(@Field('course_id') int courseId);

  // 删除课程收藏
  @override
  @DELETE('/v1/course/favorite')
  Future<SimpleResponse> removeFavoriteCourse(@Field('course_id') int courseId);

  // 添加课程到购物车
  @override
  @POST('/v1/course/shopping_cart')
  Future<SimpleResponse> addCourseToShoppingCart(
    @Field('course_id') int courseId,
    @Field('price') double price,
  );

  // 删除购物车中的课程
  @override
  @DELETE('/v1/course/shopping_cart')
  Future<PurchaseCourseResponse> removeCourseFromShoppingCart(
    @Field('course_id_array') List<int> courseIds,
  );

  // 购买课程
  @override
  @POST('/v1/course/order')
  Future<PurchaseCourseResponse> purchaseCourse(
    @Field('course_id_array') List<int> courseIds,
  );

  // 获取课程订单
  @override
  @GET('/v1/course/orders')
  Future<CourseOrderListResponse> getCourseOrders({
    @Query('course_ids') required List<int> courseIds,
    @CancelRequest() CancelToken? cancelToken,
  });

  // 正在购买的课程列表
  @override
  @GET('/v1/course/purchasing')
  Future<PurchasingCourseListResponse> getPurchasingCourses({
    @Query('offset') int? offset,
    @Query('page') int? page,
    @Query('page_size') int? pageSize,
    @CancelRequest() CancelToken? cancelToken,
  });
}
